<template>
	<view class="box" :style="theme">
		<!-- 项目类型 -->
		<view class="box_config">
			<u-tabs :list="tabList" lineHeight="1" scrollable
				:activeStyle="{fontWeight: 'bolder',fontSize: '28rpx',color: '#222222'}"
				:inactiveStyle="{fontSize: '28rpx'}" @click="tabclick" :current="active"></u-tabs>

			<scroll-view scroll-x v-if="table&&table.length>0" class="box_tabls">
				<view class="box_tabls_left">
					<view class="box_tabls_left_item" :class="projectId == item.id ? 'active' : ''"
						v-for="(item, index) in table" :key="index" @click="clickTbas(index)">{{item.projectName}}
					</view>
				</view>
				<!-- <view v-if="isOpen" class="box_tabls_right">
						<u-icon :name="isOpen?'arrow-down':'arrow-up'" @click="clickIcon" />
					</view> -->
			</scroll-view>
		</view>

		<!-- 票 -->
		<view class="box_piao">
			<view class="box_piao_item" v-for="(item, index) in list" :key="index"
				@click="jump2('/pages/ticket/ticketPurchaseDetails?id=',item.id + '&date=' + tabList[active].value)">
				<view class="box_piao_item_left">
					<image :src="item.ticketPic?item.ticketPic:imgHost+'/buy_ic_ticket.png'"></image>
					<view class="box_piao_item_left_t">
						<view>{{item.name}}</view>
						<view>{{item.projectTime}}</view>
						<view>仅限{{tabList[active].value}}{{item.duration?'，使用时长：' + item.duration+'小时' :''}}</view>
					</view>
				</view>
				<view class="box_piao_item_right" :class="item.price && Number(item.price) >0?'':'f28i green'">
					<view>{{item.price && Number(item.price) >0?'￥' +item.price:item.deposit&&Number(item.deposit)>0?'':'免费' }}</view>
					<view v-if="item.underlinedPrice" class="underlinedPrice">{{item.underlinedPrice}}</view>
				</view>
			</view>
			<u-loadmore v-if="end && list.length >= 10" :status="status" line />
		</view>
		<view v-if="list.length === 0" class="no-order">
			<image :src="imgHost+'/default_nodata.png'"></image>
			<view>暂无数据</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				table: [],
				tabList: [],
				list: [],
				page: 1,
				status: 'nomore',
				rows: 10,
				noClick: true,
				end: false,
				projectId: '',
				isOpen: true,
				active: 0,
				date: '',
				ts: 7
			}
		},
		async onLoad(ops) {
			await this.$onLaunched;
			this.firstQueryTime = this.$app.firstQueryTime()
			this.projectId = ops.id || ''
			this.date = ops.date || ''
			const scene = decodeURIComponent(ops.scene)
			if (scene && scene != 'undefined') {
				uni.setStorage({
					key: 'shareMemberId',
					data: scene
				});
				this.addShareMemberRelevance(scene)
			}
		},
		onShow() {
			if (this.userInfo && uni.getStorageSync('shareMemberId') && uni.getStorageSync('shareMemberId') !=
				'undefined') {
				this.addShareMemberRelevance(uni.getStorageSync('shareMemberId'))
			}
			this.getPz()
		},
		methods: {
			jump(url, params, a) {
				this.$app.jump(url, params, a);
			},
			jump2(url, params, a) {
				this.$app.jump2(url, params, a);
			},
			getPz() {
				this.$app.ajax({
					data: {
						param: {
							type: '7',
							code: 'TICKET_BOOK_DAY'
						}
					},
					api: this.$api.paymentConfiguration()
				}).then((res) => {
					if (res.errorCode === '000000' && res.recordList && res.recordList.length > 0) {
						this.ts = Number(res.recordList[0].value)
						this.setDate(new Date())
					}
				}).catch(() => {})
			},
			setDate(date) {
				var week = date.getDay() - 1
				if (week === -1) {
					week = 6
				}
				const arr = []
				for (var i = 0; i < this.ts; i++) {
					arr[i] = this.formatDate(i === 0 ? date : this.addDate(date, 1))
				}
				let active = 0
				arr.forEach((e, index) => {
					if (this.date && e.value === this.date) {
						active = index
					}
					if (index === 0) {
						e.name = '今天'
					}
				})
				this.active = active
				this.tabList = arr
				this.getProject()
			},
			addDate(date, n) {
				date.setDate(date.getDate() + n)
				return date
			},
			formatDate(date) {
				const year = date.getFullYear() + '-'
				const month = this.calculation(date.getMonth() + 1)
				const day = this.calculation(date.getDate())
				return {
					name: month + '-' + day,
					value: year + month + '-' + day
				}
			},
			// 获取星期
			calculation(s) {
				return s < 10 ? '0' + s : s
			},
			tabclick(e) {
				this.active = e.index;
				this.list = [];
				this.page = 1;
				this.end = false;
				this.getList()
			},
			addShareMemberRelevance(shareMemberId) {
				this.$app.ajax({
					data: {
						param: {
							shareMemberId
						}
					},
					api: this.$api.addShareMemberRelevance()
				}).then((res) => {
					if (res.errorCode === '000000') {
						uni.removeStorageSync('shareMemberId');
					}
				})
			},
			// 点击箭头
			clickIcon() {
				this.isOpen = !this.isOpen
			},
			clickTbas(index) {
				this.projectId = this.table[index].id
				this.list = [];
				this.page = 1;
				this.end = false;
				this.getList()
			},
			getProject() {
				this.$app.ajax({
					data: {
						param: {
							venueId: this.venueId,
							type: "1",
							status: '1'
						}
					},
					api: this.$api.getProject()
				}).then((res) => {
					if (res.errorCode === '000000') {
						this.table = [{
							projectName: '全部',
							id: ''
						}, ...res.recordList]
						this.list = [];
						this.page = 1;
						this.end = false;
						this.getList()
					}
				}).catch(() => {})
			},
			getList() {
				this.$app.ajax({
					api: this.$api.getTickets(),
					data: {
						pagination: {
							page: this.page,
							firstQueryTime: this.firstQueryTime,
							rows: this.rows
						},
						param: {
							venueId: this.venueId,
							projectId: this.projectId,
							saleDate: this.tabList[this.active].value
						}
					}
				}).then((res) => {
					if (res.errorCode === '000000') {
						if (!res.recordList || res.recordList.length < this.rows) {
							this.end = true
						}
						if (res.recordList) {
							this.list = this.list.concat(res.recordList)
						}
					}
				}).catch(() => {})
			},
			reachBottom() {
				if (!this.end) {
					this.page += 1;
					this.getList();
				}
			},
			// 触底加载更多数据
			onReachBottom(e) {
				this.reachBottom()
			}

		}

	}
</script>
<style lang="scss" scoped>
	.box_config {
		position: sticky;
		top: 0;
		background: #ffffff;
		left: 0;
		right: 0;
		z-index: 999;
	}

	.box_tabls {
		display: flex;
		/* align-items: center; */
		justify-content: space-between;
		padding: 30rpx;
		background: #ffffff;
		// position: sticky;
		// top: 0;
		// left: 0;
		// right: 0;
		overflow: hidden;

		.box_tabls_left {
			white-space: nowrap;
			display: flex;

			.box_tabls_left_item {
				padding: 0 46rpx;
				line-height: 70rpx;
				border-radius: 35rpx;
				font-size: 24rpx;
				font-weight: 400;
				color: #111111;
				background: $bjColor;
				margin-right: 16rpx;

				&:last-of-type {
					margin-right: 0;
				}

				&.active {
					color: #ffffff;
					background: $color;
				}
			}
		}
	}

	.box_piao {
		padding: 24rpx 30rpx;
		box-sizing: border-box;
	}

	.box_piao_item {
		padding: 30rpx;
		height: 180rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 24rpx;
	}

	.box_piao_item_left image {
		width: 120rpx;
		height: 120rpx;
		flex-shrink: 0;
	}

	.box_piao_item_left_t {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-left: 30rpx;
		width: calc(100% - 150rpx);
	}

	.box_piao_item_left {
		width: 80%;
		display: flex;
		overflow: hidden;
	}

	.box_piao_item_left_t view:nth-child(1) {
		width: 348rpx;
		font-size: 30rpx;
		font-weight: 500;
		color: #222222;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.box_piao_item_left_t view:nth-child(2) {
		font-size: 24rpx;
		font-weight: 400;
		color: #222222;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.box_piao_item_left_t view:nth-child(3) {
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
	}

	.box_piao_item_right {
		flex: 1;
		text-align: right;
		font-size: 30rpx;
		font-weight: 600;
		color: $red;
	}
</style>